#!/bin/bash

# 雷达系统服务管理脚本
# 提供便捷的服务管理命令

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 工作目录
WORK_DIR="/home/admin1/lidar"
LOG_DIR="$WORK_DIR/logs"

# 显示帮助信息
show_help() {
    echo -e "${CYAN}雷达系统服务管理工具${NC}"
    echo ""
    echo "用法: $0 <命令>"
    echo ""
    echo -e "${GREEN}服务控制命令:${NC}"
    echo "  start     - 启动所有服务"
    echo "  stop      - 停止所有服务"
    echo "  restart   - 重启所有服务"
    echo "  status    - 查看服务状态"
    echo ""
    echo -e "${GREEN}日志查看命令:${NC}"
    echo "  logs      - 查看后端实时日志"
    echo "  logs-tail - 查看后端日志尾部"
    echo "  logs-error- 查看错误日志"
    echo ""
    echo -e "${GREEN}测试命令:${NC}"
    echo "  test      - 测试API接口"
    echo "  test-ptz  - 测试云台控制"
    echo "  test-relay- 测试继电器控制"
    echo ""
    echo -e "${GREEN}系统命令:${NC}"
    echo "  install   - 安装为系统服务"
    echo "  uninstall - 卸载系统服务"
    echo "  enable    - 启用开机自启"
    echo "  disable   - 禁用开机自启"
    echo ""
    echo -e "${GREEN}其他命令:${NC}"
    echo "  help      - 显示此帮助信息"
    echo "  version   - 显示版本信息"
}

# 显示版本信息
show_version() {
    echo -e "${CYAN}雷达系统服务管理工具 v1.0${NC}"
    echo "构建日期: $(date +%Y-%m-%d)"
    echo "工作目录: $WORK_DIR"
}

# 测试API接口
test_api() {
    echo -e "${BLUE}测试API接口...${NC}"
    
    echo "1. 测试通道列表API:"
    curl -s "http://localhost:5000/test/channel/list" | head -3 || echo "请求失败"
    
    echo ""
    echo "2. 测试云台角度API:"
    curl -s "http://localhost:5000/test/ptz/list" | head -3 || echo "请求失败"
    
    echo ""
    echo "3. 测试继电器状态API:"
    curl -s "http://localhost:5000/test/relay/list" | head -3 || echo "请求失败"
}

# 测试云台控制
test_ptz() {
    echo -e "${BLUE}测试云台控制...${NC}"
    
    echo "发送PAN命令 (水平角度180°):"
    curl -X POST "http://localhost:5000/test/ptz/sendSpinCmd" \
         -H "Content-Type: application/json" \
         -d '{"channelName":"0000000000000000","address":30,"direction":"PAN","angle":180}' || echo "请求失败"
    
    echo ""
    echo "等待3秒..."
    sleep 3
    
    echo "发送TILT命令 (垂直角度45°):"
    curl -X POST "http://localhost:5000/test/ptz/sendSpinCmd" \
         -H "Content-Type: application/json" \
         -d '{"channelName":"0000000000000000","address":30,"direction":"TILT","angle":45}' || echo "请求失败"
}

# 测试继电器控制
test_relay() {
    echo -e "${BLUE}测试继电器控制...${NC}"
    
    echo "打开继电器:"
    curl -X POST "http://localhost:5000/test/relay/doControl" \
         -H "Content-Type: application/json" \
         -d '{"channelName":"d4ad2070b92f0000","address":1,"doNumber":1,"f":true}' || echo "请求失败"
    
    echo ""
    echo "等待3秒..."
    sleep 3
    
    echo "关闭继电器:"
    curl -X POST "http://localhost:5000/test/relay/doControl" \
         -H "Content-Type: application/json" \
         -d '{"channelName":"d4ad2070b92f0000","address":1,"doNumber":1,"f":false}' || echo "请求失败"
}

# 查看实时日志
view_logs() {
    echo -e "${BLUE}查看后端实时日志 (按Ctrl+C退出):${NC}"
    tail -f "$LOG_DIR/backend.log"
}

# 查看日志尾部
view_logs_tail() {
    echo -e "${BLUE}后端日志尾部 (最近50行):${NC}"
    tail -50 "$LOG_DIR/backend.log"
}

# 查看错误日志
view_logs_error() {
    echo -e "${BLUE}查找错误日志:${NC}"
    grep -i "error\|exception\|failed" "$LOG_DIR/backend.log" | tail -20 || echo "未发现错误日志"
}

# 安装系统服务
install_service() {
    echo -e "${BLUE}安装系统服务...${NC}"
    
    if [ "$EUID" -ne 0 ]; then
        echo -e "${RED}需要root权限安装系统服务${NC}"
        echo "请运行: sudo $0 install"
        exit 1
    fi
    
    cp "$WORK_DIR/lidar-system.service" /etc/systemd/system/
    systemctl daemon-reload
    systemctl enable lidar-system.service
    
    echo -e "${GREEN}系统服务安装成功！${NC}"
    echo "使用以下命令管理服务:"
    echo "  sudo systemctl start lidar-system"
    echo "  sudo systemctl stop lidar-system"
    echo "  sudo systemctl status lidar-system"
}

# 卸载系统服务
uninstall_service() {
    echo -e "${BLUE}卸载系统服务...${NC}"
    
    if [ "$EUID" -ne 0 ]; then
        echo -e "${RED}需要root权限卸载系统服务${NC}"
        echo "请运行: sudo $0 uninstall"
        exit 1
    fi
    
    systemctl stop lidar-system.service || true
    systemctl disable lidar-system.service || true
    rm -f /etc/systemd/system/lidar-system.service
    systemctl daemon-reload
    
    echo -e "${GREEN}系统服务卸载成功！${NC}"
}

# 主函数
main() {
    cd "$WORK_DIR"
    
    case "${1:-help}" in
        "start")
            ./start_services.sh start
            ;;
        "stop")
            ./start_services.sh stop
            ;;
        "restart")
            ./start_services.sh restart
            ;;
        "status")
            ./start_services.sh status
            ;;
        "logs")
            view_logs
            ;;
        "logs-tail")
            view_logs_tail
            ;;
        "logs-error")
            view_logs_error
            ;;
        "test")
            test_api
            ;;
        "test-ptz")
            test_ptz
            ;;
        "test-relay")
            test_relay
            ;;
        "install")
            install_service
            ;;
        "uninstall")
            uninstall_service
            ;;
        "enable")
            sudo systemctl enable lidar-system.service
            echo -e "${GREEN}开机自启已启用${NC}"
            ;;
        "disable")
            sudo systemctl disable lidar-system.service
            echo -e "${GREEN}开机自启已禁用${NC}"
            ;;
        "version")
            show_version
            ;;
        "help"|*)
            show_help
            ;;
    esac
}

# 执行主函数
main "$@"
